#define _CRT_SECURE_NO_WARNINGS 1

//#include<iostream>
//#include<cstring>
//
//using namespace std;
//
//const int N = 110;
//
//int f[N][N];
//int n, m, k;
//
//int main()
//{
//    cin >> n >> m >> k;
//    memset(f, 0x3f, sizeof f);
//    f[0][0] = 0;
//    while (k--)
//    {
//        int a, b, c;
//        cin >> a >> b >> c;
//        for (int i = n; i >= 0; i--)
//            for (int j = m; j >= 0; j--)
//                f[i][j] = min(f[i][j], f[max(0, i - a)][max(0, j - b)] + c);
//    }
//    cout << f[n][m] << endl;
//    return 0;
//}

#include<iostream>

using namespace std;

const int N = 11, M = 16;

int n, m;
int f[N][M];
int w[N][M];
int way[N];

int main()
{
    cin >> n >> m;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> w[i][j];

    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= m; j++)
            for (int k = 0; k <= j; k++)
                f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);
    cout << f[n][m] << endl;

    int j = m;
    for (int i = n; i; i--)
        for (int k = 0; k <= j; k++)
            if (f[i][j] == f[i - 1][j - k] + w[i][k])
            {
                way[i] = k;
                j -= k;
                break;
            }

    for (int i = 1; i <= n; i++)   cout << i << ' ' << way[i] << endl;
    return 0;
}